﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Facade;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.ProductLibrary
{
    public partial class ManageBenefit : SOSecurityPage
    {
        private String action = "";
        private static string codeIdd = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ValidateUserCredentials(Request.Url.LocalPath);

                if (!String.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    action = Request.QueryString["a"].ToString();
                }

                if (action == "u")
                {
                    this.LoadData();
                }
                else if (action == "v")
                {
                    this.LoadData();
                    //btnUpdate.Visible = false;
                    btnUp.Attributes["style"] = "display:none";
                    //this.FindControl("cellUpdate").Visible = false;
                }
            }
        }

        private void LoadData()
        {
            try
            {
                string id1 = (Session["benefitCode"].ToString());
                FinanceFacade benefitfacade = new FinanceFacade();
                List<QuoLifeBenefit> benefit = benefitfacade.GetBenefit(id1, 1);

                codeIdd = benefit.First().BenefitCode;
                lblBenefitCode.Text = benefit.First().BenefitCode;
                lblBenefitName.Text = benefit.First().BenefitName;

                if (action == "u")
                {
                    txtDependentGrp.Text = (benefit.First().DefendentGrp).ToString();
                    lblDependentGRP.Visible = false;
                }
                else if (action == "v")
                {
                    lblDependentGRP.Text = (benefit.First().DefendentGrp).ToString();
                    txtDependentGrp.Visible = false;
                }


                if (action == "v")
                {
                    rdoExpireNil.Enabled = false;
                    rdoExpireNo.Enabled = false;
                    rdoExpireYes.Enabled = false;

                    if (benefit.First().BenefitExpireStatus == "Y")
                    {
                        rdoExpireYes.Checked = true;

                    }
                    else if (benefit.First().BenefitExpireStatus == "N")
                    {
                        rdoExpireNo.Checked = true;

                    }
                    else
                    {
                        rdoExpireNil.Checked = true;


                    }
                }
                else
                {

                    if (benefit.First().BenefitExpireStatus == "Y")
                    {
                        rdoExpireYes.Checked = true;
                    }
                    else if (benefit.First().BenefitExpireStatus == "N")
                    {
                        rdoExpireNo.Checked = true;
                    }
                    else
                    {
                        rdoExpireNil.Checked = true;

                    }

                }



                if (action == "v")
                {
                    rdoNoPDStatus.Enabled = false;
                    rdoYesPDStatus.Enabled = false;
                    rdoNilPDStatus.Enabled = false;

                    if (benefit.First().PdSt == "Y")
                    {
                        rdoYesPDStatus.Checked = true;
                    }
                    else if (benefit.First().PdSt == "N")
                    {
                        rdoNoPDStatus.Checked = true;
                    }
                    else
                    {
                        rdoNilPDStatus.Checked = true;

                    }
                }
                else
                {

                    if (benefit.First().PdSt == "Y")
                    {
                        rdoYesPDStatus.Checked = true;
                    }
                    else if (benefit.First().PdSt == "N")
                    {
                        rdoNoPDStatus.Checked = true;
                    }
                    else
                    {
                        rdoNilPDStatus.Checked = true;

                    }

                }

                if (action == "v")
                {
                    rdoNilPMStatus.Enabled = false;
                    rdoNoPMStatus.Enabled = false;
                    rdoYesPMStatus.Enabled = false;

                    if (benefit.First().PmSt == "Y")
                    {
                        rdoYesPMStatus.Checked = true;
                    }
                    else if (benefit.First().PmSt == "N")
                    {
                        rdoNoPMStatus.Checked = true;
                    }
                    else
                    {
                        rdoNilPMStatus.Checked = true;

                    }
                }
                else
                {

                    if (benefit.First().PmSt == "Y")
                    {
                        rdoYesPMStatus.Checked = true;
                    }
                    else if (benefit.First().PmSt == "N")
                    {
                        rdoNoPMStatus.Checked = true;
                    }
                    else
                    {
                        rdoNilPMStatus.Checked = true;

                    }


                }

                if (action == "v")
                {
                    rdoYesIBRVStatus.Enabled = false;
                    rdoNoIBRVStatus.Enabled = false;
                    rdoNilIBRVStatus.Enabled = false;

                    if (benefit.First().IbrvSt == "Y")
                    {
                        rdoYesIBRVStatus.Checked = true;
                    }
                    else if (benefit.First().IbrvSt == "N")
                    {
                        rdoNoIBRVStatus.Checked = true;
                    }
                    else
                    {
                        rdoNilIBRVStatus.Checked = true;

                    }
                }
                else
                {
                    if (benefit.First().IbrvSt == "Y")
                    {
                        rdoYesIBRVStatus.Checked = true;
                    }
                    else if (benefit.First().IbrvSt == "N")
                    {
                        rdoNoIBRVStatus.Checked = true;
                    }
                    else
                    {
                        rdoNilIBRVStatus.Checked = true;

                    }

                }


            }
            catch (Exception ex)
            {
                return;
            }

        }

        private void Update()
        {
            FinanceFacade fFac = new FinanceFacade();
            QuoLifeBenefit qlb = fFac.GetOneBenefit(Session["benefitCode"].ToString());
            qlb.BenefitName = lblBenefitName.Text;
            int DependentGrp = Convert.ToInt32((txtDependentGrp.Text).ToString());

            qlb.DefendentGrp = DependentGrp;

            if (rdoExpireYes.Checked == true)
            {
                qlb.BenefitExpireStatus = "Y";
            }
            else if (rdoExpireNo.Checked == true)
            {
                qlb.BenefitExpireStatus = "N";
            }
            else
            {
                qlb.BenefitExpireStatus = "";
            }


            if (rdoYesPDStatus.Checked == true)
            {
                qlb.PdSt = "Y";
            }
            else if (rdoNoPDStatus.Checked == true)
            {
                qlb.PdSt = "N";
            }
            else
            {
                qlb.PdSt = "";
            }


            if (rdoYesPMStatus.Checked == true)
            {
                qlb.PmSt = "Y";
            }
            else if (rdoNoPMStatus.Checked == true)
            {
                qlb.PmSt = "N";
            }
            else
            {
                qlb.PmSt = "";
            }



            if (rdoYesIBRVStatus.Checked == true)
            {
                qlb.IbrvSt = "Y";
            }
            else if (rdoNoIBRVStatus.Checked == true)
            {
                qlb.IbrvSt = "N";
            }
            else
            {
                qlb.IbrvSt = "";
            }

            int successUpdate = fFac.UpdateBenefit(qlb);

            if (successUpdate == 1)
            {
                Session["successCodeUpdate"] = "Successfully updated ";
            }
            else
            {
                Session["failCodeUpdate"] = "Failed to update ";
            }

        }

        protected void RadioButton5_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            if (Request.QueryString["a"] == "u")
            {
                Update();
                Session["benefitCode"] = null;
                Response.Redirect("~/ProductLibrary/SearchBenefits.aspx?update= " + Request.QueryString["update"]);
            }
            else if (Request.QueryString["a"] == "v")
            {

            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["a"] == "u")
            {
                Session["benefitCode"] = null;
                Response.Redirect("~/ProductLibrary/SearchBenefits.aspx?update= " + Request.QueryString["update"]);
            }
            else if (Request.QueryString["a"] == "v")
            {
                Session["benefitCode"] = null;
                Response.Redirect("~/ProductLibrary/SearchBenefits.aspx?view= " + Request.QueryString["view"]);
            }
        }
    }
}
